package com.ljw.spring.annotation.ext;

import com.ljw.spring.annotation.bean.Desk;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * @FileName MyBeanDefinitionRegistryPostProcessor
 * @Description TODO
 * @Author ljw
 * @Date 2021/2/4 2:14
 * @Version 1.0
 */
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    //BeanDefinitionRegistry： bean定义信息的保存，以后beanFactory就是按照BeanDefinitionRegistry中的bean定义信息实例化bean
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println("MyBeanDefinitionRegistryPostProcessor----方法postProcessBeanDefinitionRegistry,bean数量："+registry.getBeanDefinitionCount());
        //指定要注册的类
        RootBeanDefinition definition = new RootBeanDefinition(Desk.class);
        //手动向IOC容器加入指定组件
        registry.registerBeanDefinition("desk",definition);
    }


    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("MyBeanDefinitionRegistryPostProcessor----方法postProcessBeanFactory,bean数量："+beanFactory.getBeanDefinitionCount());
    }
}
